<?php
require_once('./modules/db.inc');
require_once('./modules/feeds/feed.inc');

class ManageFeedsRenderer extends Renderer
{
	function pageLoad()
	{
		switch( $this->action )
		{
			case "get_tags":
				$this->getTags();
				break;
			case "add_tag":
				$this->addTag();
				break;
			case "del_tag":
				$this->delTag();
				break;
			case "del_feed":
				$this->delFeed();
				break;
			case "":
			case "show":
			default:
				break;
		}
	}

	function delFeed()
	{
		$feed = new Feed();
		$feed->delete($_GET["feedId"]);
	}	
	function delTag()
	{
		$feed = new Feed();
		$feed->deleteTag($_GET["feedId"],$_GET["tagId"]);
		die();
	}

	function addTag()
	{
		$feedTag = new Feed();
		$feedTag->addTag($_GET["feedId"],$_GET["tagId"]);
		die();
	}
	
	function getTags()
	{
		$result = "{Tags:[";
		
		$feed = new Feed();
		$data = $feed->getTagCloud($_GET["feedId"]);
		
		$first = true;
		foreach($data as $row)
		{
			if(!$first)
				$result .= ",";
			$first = false;

			$class = isset($row['tag_id']) ?"tagItem tagItemSelected":"tagItem";
			
			$result .= "{ tag : '".$row['tag']."',id:".$row['id'].", className:'".$class."'}";			
		}
		
		$result .= "] }";
		
		die($result);
	}
	
	function isPermitted()
	{
		//echo "permission check feeds";
		if ( isset($this->user) && $this->user->type == 2 )
			return true;
		else
			return false;
	}
	
	function renderContent()
	{
		global $content;
		
		$content .= "
		<script src='./script/popup.js'></script>
		<script>
			function setTag(elem,rowId)
			{
				var tagId = elem.getAttribute('tagId');
				elem = $('span[tagId=\''+tagId+'\']');
				if(elem.hasClass('tagItemSelected'))
				{
					elem.removeClass('tagItemSelected');
					$.get(document.location.href.replace(/\?.*/ig,\"?pg=manage_feeds&act=del_tag&feedId=\"+rowId+\"&tagId=\"+tagId));
				}
				else
				{
					elem.addClass('tagItemSelected');
					$.get(document.location.href.replace(/\?.*/ig,\"?pg=manage_feeds&act=add_tag&feedId=\"+rowId+\"&tagId=\"+tagId));
				}
			}

			function showTag(rowId)
			{
				var row = $('#tr'+rowId);

				$('#trtg').remove();
					
				row.after('<tr id=\'trtg\' ><td id=\'tdTags\' style=\'line-height:1.8em\' colspan=\'5\'>loading tag cloud...</td></tr>');
				
				var reqUrl = document.location.href.replace(/\?.*/ig,\"?pg=manage_feeds&act=get_tags&feedId=\"+rowId);
				$.getJSON( reqUrl,
					function(data)
					{
						$('#tdTags').html(data.Tags.length > 0 ? '' : 'No current tags');
						$.each(data.Tags, function(i,tag)
						{
							$('<span onclick=\"setTag(this,'+rowId+');\";/>').attr('class',tag.className).attr('tagId',tag.id).html(tag.tag).appendTo('#tdTags');
							$('<WBR/>').appendTo('#tdTags');
						});
					});
			}
			
			function askDelFeed(feedId,feedName)
			{
				_popup.show('Should I delete feed '+feedName+'?',delFeed,feedId)
			}
			
			function delFeed(isYes,feedId)
			{
				if(isYes)
				{
					var reqUrl = document.location.href.replace(/\?.*/ig,\"?pg=manage_feeds&act=del_feed&feedId=\"+feedId);
					$.get(reqUrl);
					$('#tr'+feedId).remove();
					$('#trtg').remove();
				}
				_popup.hide();

			}
			
			function editFeed(feedId,feedName)
			{
				var reqUrl = document.location.href.replace(/\?.*/ig,\"?pg=edit_feed&feedId=\"+feedId);
				document.location.href=reqUrl;
			}
		</script>";
		
		$content .= 
		"<p><h1>Manage feeds</h1></p>
		<div>
			<table id='tFeeds' style='table-layout:fixed' border='1'>
				<thead>
					<tr>
						<th style='width:40px'>Name</th>
						<th style='width:180px'>Description</th>
						<th style='width:240px'>Url</th>
						<th style='width:20px'><img src='./images/blank.gif'/></th>
						<th style='width:20px'><img src='./images/blank.gif'/></th>
					</tr>
				</thead>
				<tbody>";

		$oFeed = new Feed();

		$feeds = $oFeed->allFeeds(false);
				
		if ($feeds == null || count($feeds) == 0) 
			$content .="<tr><td colspan='4'>".mysql_error()."</td></tr>";

		foreach($feeds as $feed)
		{
			$feed->feedName = str_replace("'","&apos;",$feed->feedName);
			
			$content .= "<tr id='tr".$feed->id."'>
							<td style='width:40px'>".$feed->feedName."</td>
							<td style='width:180px'>".$feed->description."</td>
							<td style='width:240px'><WBR/>".$feed->url."</td>
							<td style='width:20px;cursor:pointer;'><img src='./images/tag.png' onclick='showTag(".$feed->id.")'></td>
							<td style='width:20px;cursor:pointer;'><img src='./images/delete.png' onclick=\"askDelFeed(".$feed->id.",'".$feed->feedName."')\"></td>
							<td style='width:20px;cursor:pointer;'><img src='./images/pencil.png' onclick=\"editFeed(".$feed->id.",'".$feed->feedName."')\"></td>
						</tr>";
		}
		$content .=	"</tbody>
			</table>
		</div>";
	}
}

$renderer = new ManageFeedsRenderer();

?>